﻿using System;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using Microsoft.Security.Application;

namespace Job.Core.Web
{
    /// <summary>
    /// 与 web 相关的 String 扩展。
    /// </summary>
    public static class StringExtensions
    {
        #region Properties

        private static readonly Regex htmlTagRegex = new Regex("<[^<>]*>", RegexOptions.Compiled | RegexOptions.Multiline);

        #endregion

        #region Methods

        /// <summary>
        /// Html 编码。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>编码后的字串。</returns>
        public static string HtmlEncode(this string s)
        {
            return HttpUtility.HtmlEncode(s);
        }

        /// <summary>
        /// Html 解码。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>解码后的字串。</returns>
        public static string HtmlDecode(this string s)
        {
            return HttpUtility.HtmlDecode(s);
        }

        /// <summary>
        /// Url 编码。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>编码后的字串。</returns>
        public static string UrlEncode(this string s)
        {
            return HttpUtility.UrlEncode(s);
        }

        /// <summary>
        /// Url 解码。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>解码后的字串。</returns>
        public static string UrlDecode(this string s)
        {
            return HttpUtility.UrlDecode(s);
        }

        /// <summary>
        /// 清除 Html 标记。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>清除标记后的字串。</returns>
        public static string CleanHtmlTags(this string s)
        {            
            return htmlTagRegex.Replace(s, string.Empty);
        }

        /// <summary>
        /// 从左开始截取指定长度字符。
        /// </summary>
        /// <param name="s"></param>
        /// <param name="len">要截取的长度。</param>
        /// <returns>截取后的字串。</returns>
        public static string Left(this string s, int len)
        {
            if (s.Length <= len)
                return s;

            return s.Substring(0, len);
        }

        /// <summary>
        /// 从左开始截取指定长度字符，当字符串超过长度时，加'..'号。
        /// </summary>
        /// <param name="s"></param>
        /// <param name="len">要截取的长度。</param>
        /// <returns>截取后的字串。</returns>
        public static string LeftX(this string s, int len)
        {
            if (s.Length <= len)
                return s;

            return s.Substring(0, len) + "..";
        }

        /// <summary>
        /// 将换行符转换为 Html 的 br 标记。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>转换后的字串。</returns>
        public static string BrEncode(this string s)
        {
            s = s.HtmlEncode();
            s = s.Replace("\r\n", "<br />");
            s = s.Replace("\r", "<br />");
            s = s.Replace("\n", "<br />");
            return s;
        }

        /// <summary>
        /// 将 Html 中的 br 标记转换成换行符。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>转换后的字串。</returns>
        public static string BrDecode(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return s;

            return s.Replace("<br />", "\n");
        }

        /// <summary>
        /// 获取安全的 html。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>安全的 html。</returns>
        public static string GetSafeHtml(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return s;

            return AntiXss.GetSafeHtmlFragment(s);
        }

        /// <summary>
        /// 获取安全的 sql 。
        /// </summary>
        /// <param name="s"></param>
        /// <returns>安全的 sql。</returns>
        public static string GetSafeKeyWord(this string s)
        {
            if (string.IsNullOrEmpty(s))
                return string.Empty;

            return s.Replace("'", "''");
        }
        #endregion
    }
}